<?php
error_reporting(E_ALL);

define('DS', DIRECTORY_SEPARATOR);
define('ROOT', (dirname(dirname(__FILE__))) . DS);

$mahees_framework_path = ROOT.'components'.DS.'emerald_framework'.DS;
require($mahees_framework_path.'framework_initializer.php');


class Route {

	private static $route_map = array();

	static function aCleanUrl($sUrl) {
		if ($sUrl === '') { return array(); }
		$aUrl = explode('/', $sUrl);
		if (end($aUrl) == '') {	array_pop($aUrl); }
		return $aUrl;
	}

	static function addToRouteTable($sTemplate, $aRoute) { self::$route_map[$sTemplate] = $aRoute; }

	static function getRouteTable() { return self::$route_map; }

}

class Dispatch {

	static function dispatcher() {
		$aGetRUrl = Route::aCleanUrl($_GET['r']); unset($_GET['r']);

		require(CONFIG.'routes.php');

		foreach(Route::getRouteTable() as $sTemplate => $aRoute) {
			$aTemplate = Route::aCleanUrl($sTemplate);
			foreach ($aGetRUrl as $key => $caiParam) {
				if (!isset($aTemplate[$key])) { continue 2; }
				if ($aTemplate[$key][0] != ':') { if (!($aTemplate[$key] == $caiParam)) { continue 2; } }
				else { $_GET[substr($aTemplate[$key], 1)] = $caiParam; }
			}
			foreach($aRoute as $k => $v) { if (!isset($_GET[$k])) { $_GET[$k] = $v; } }
			//should have atleast matched ':controller/:action/:id'
			//making sure ca are set
			if (!isset($_GET['controller'])) { $_GET['controller'] = 'public'; }
			if (!isset($_GET['action'])) { $_GET['action'] = 'index'; }
			return self::execute_controller_action($_GET['controller'], $_GET['action']);
		}
		
	}

	static function connect($sTemplate, $aRoute = array()) { Route::addToRouteTable($sTemplate, $aRoute); }

	static function execute_controller_action($controller, $action) {
		//echo $controller, ' - ', $action;
		$controllerClassName = ucfirst($controller).'_controller';
		$controller_file = APPLICATION_PATH.'controllers'.DS.$controllerClassName.'.php';

		if (file_exists($controller_file)) {
			require($controller_file);
			if ($controller_instance = new $controllerClassName()) {
				if (method_exists($controller_instance, $action)) {	if (!$controller_instance->$action()) { return $controller_instance->render(); } }
				echo 'action DOESNT exist in specified class'; return false;
			}
			echo 'no class found! or couldnt create instance of it!'; return false;
		}
		echo 'controller file not found'; return false;
	}


}


Dispatch::dispatcher();


//require(CONFIG . 'routes.php');
////print_r(Route::get_routes());
//echo url_for(array('controller' => 'staff', 'action' => 'sslogin', 'id'=>'bear', 'cat' => 'dog', 'b' => 'c'));
//echo '<br />';
//echo url_for(array('controller' => 'staffss'));
//echo '<br />';
//echo url_for(array('controller' => 'staff'));
//echo '<br />';
//echo url_for(array('action' => 'productCompare', 'list' => 'apple', 'cat' => 'dog'));
//echo '<br />';
//echo url_for(array('action' => 'login'));
//echo '<br />';
//echo url_for(array('controller' => 'staff', 'action' => 'login'));
//echo '<br />';
//echo url_for(array('action' => 'productList', 'type' => 'apple'));
//echo '<br />';
//echo url_for(array('action' => 'productList'));
//echo '<br />';
//echo url_for(array('action' => 'productCompare', 'list' => 'apple'));
//echo '<br />';

//print_r($map);
//echo 'boiii, ur routes dont match anything! from Dispatch';

?>